import requests
from bs4 import BeautifulSoup


def fetch_page():
    # 使用最新的已知正确地址
    url = 'https://www.pxc.jx.cn'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }

    try:
        # 添加 timeout 避免卡死，设置最大连接时间与读取时间
        response = requests.get(url, headers=headers, timeout=(5, 10))  # 连接5秒超时，读取10秒超时

        if response.status_code == 200:
            response.encoding = response.apparent_encoding

            with open("D:\\萍乡学院.html", "w", encoding='utf-8') as file:
                soup = BeautifulSoup(response.text, 'html.parser')
                file.write(soup.prettify())

            print("页面保存成功")
        else:
            print(f"请求失败，状态码：{response.status_code}")

    except requests.exceptions.Timeout:
        print("请求超时，请检查网络连接或目标服务器是否响应缓慢。")

    except requests.exceptions.ConnectionError:
        print("连接失败，无法解析目标域名，请检查网址是否正确或网络是否通畅。")

    except Exception as e:
        print(f"发生未知错误：{e}")


if __name__ == '__main__':
    fetch_page()